from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.db import connection
import requests
import json
import os
from django.http import HttpResponseRedirect
from django.urls import reverse


def get_all_path(path):  # 传入存储的list
    temp_list=[]
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if not os.path.isdir(file_path):
            temp_list.append(file_path)
    return temp_list

def get_describe_data(describe_txt_path_):
    f = open(describe_txt_path_, "r", encoding="UTF-8")
    strrr = f.read()
    f.close()
    return strrr

def get_id_and_describe(request):
    print("temp_test  temp_test  temp_test")
    print("temp_test  temp_test  temp_test")
    print("temp_test  temp_test  temp_test")
    print("temp_test  temp_test  temp_test")
    print("temp_test  temp_test  temp_test")
    my_dict = dict()  # 使用dict()函数创建空字典
    print(my_dict)  # 输出: {} 
    file_list_string = ''
    file_path=os.path.abspath(__file__).split("index.py")[0] + "templates\html_demo\\" 
    for ii in get_all_path(file_path):
        if ".txt" in ii.strip():
            describe_txt_path = ii.strip()
            file_ID = ii.split("\\")[-1]
            file_ID = file_ID.split(".")[0]
            # print(describe_txt_path)
            # print(file_ID)
            describe_data = get_describe_data(describe_txt_path)
            # print(describe_data)
            print()
            my_dict[file_ID] = describe_data
    print(my_dict)
    file_list_string = file_list_string + ii.strip().replace(".txt", "") + ","
    file_list_string = file_list_string.strip()
    file_list_string = file_list_string[:-1]
    print(file_list_string)
    return HttpResponse("哈哈哈哈哈哈哈哈哈", content_type="application/json")    

from django.shortcuts import redirect
def index_redirect(request):
    return redirect('/index/all')

# 首页初始化所有文件列表:  
def index(request, type):
    my_dict = dict()        # 使用dict()函数创建空字典
    print(type) # all   haveImg    collected
    file_path=os.path.abspath(__file__).split("index.py")[0] + "templates\html_demo\\" 
    for ii in get_all_path(file_path):
        if ".txt" in ii.strip():
            describe_txt_path = ii.strip()
            file_ID = ii.split("\\")[-1]
            file_ID = file_ID.split(".")[0]
            # print(describe_txt_path)
            # print(file_ID)
            describe_data = get_describe_data(describe_txt_path)
            # print(describe_data)
            # print()
            if type == "haveImg":
                if "--haveImg" in describe_data:
                    my_dict[file_ID] = describe_data
            if type == "collected":
                if "--collected" in describe_data:
                    my_dict[file_ID] = describe_data
            if type == "test":
                if "--TEST" in describe_data:
                    my_dict[file_ID] = describe_data            
            if type == "all":
                my_dict[file_ID] = describe_data
    print(my_dict)
    print(len(my_dict))
    resp = {
        'file_list':json.dumps(my_dict),
        'return_size' : len(my_dict)
    }
    return render(request, 'index/inde.html', context=resp)




def get_html_txt_describe(file_id , type__):
    if type__ == "txt":
        file_name = file_id + ".txt"
    if type__ == "html":
        file_name = file_id + ".html"
    file_path = os.path.abspath(__file__).split("index.py")[0] + "templates\html_demo\\" + file_name
    print(file_path)
    strrrr = ""
    with open(file_path, "r", encoding="utf-8") as f:
        strrrr = f.read()
    print(strrrr)
    return strrrr


@csrf_exempt
def get_file_data(request):
    # myfile = request.FILES['myfile']  // input 文件类型获取数据
    global data
    if request.method == 'POST':
        print("  get HTML txt  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        print(request.body)       # b'user-name=1&user-email=1%40q.com'
        data = {}
        file_name = request.POST.get('file_name')
        print(file_name)
        file_id = file_name.split(".")[0]
        html_data = get_html_txt_describe(file_id , "html")
        txt_describe = get_html_txt_describe(file_id , "txt")
        data['html_data']=html_data
        data['describe']=txt_describe
        print(data)
        data=json.dumps(data)
        print(type(data))
    return HttpResponse(data, content_type="application/json")    



@csrf_exempt
def xxxxxxxxxxxxxxxxx(request):
    if request.method == 'POST':
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        print(request.body)       # b'user-name=1&user-email=1%40q.com'
        data = {}
        file_name = request.POST.get('file_name')
        file_content = request.POST.get('file_content')
        print(file_name)
        print(file_content)
        data = json.dumps(data)
        print(data)
        print(type(data))
    return HttpResponse(data, content_type="application/json")    

 